<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * @param {Object|Array} obj
       * @return {Object|Array}
       */
      var compactObject = function (obj) {
        let finalObj = obj instanceof Array ? [] : {};
        for (key in obj) {
          if (obj[key]) {
            finalObj[key] =
              typeof obj[key] === "object" ? compactObject(obj[key]) : obj[key];
          }
        }
        return obj instanceof Array
          ? finalObj.filter((e) => e != null)
          : finalObj;
      };
    </script>
  </body>
</html>
